var customerId;
loadInitData();
function loadInitData() {
    var token = localStorage.getItem("token");
    console.log("token:"+token);
    $.ajax({
        type: "get",
        url: "/demo/getCustomerTel",
        headers: {'token': token},
        success: function (result) {
            var map=result.data;
            if (map!=null){
                var customerTel=map.customerTel;
                customerId=map.customerId;
                var url = "/demo/order?customerId=" + customerId;
                $(".head-user").hide();
                $(".user-menu").html('<li class="text"><a href='+url+'>个人中心</a></li>\n' +
                    '                 <li class="text" id="return"><button style="border: none">退出登录</button></li>');
                $(".user-info").show();
                $(".phone-num").text(customerTel);
            }else {
                $(".head-user").show();
                $(".user-info").hide();
            }
            $("#return").click(function (){
                var token = localStorage.removeItem("token");
                window.location.href = "http://localhost:8080/demo/index.jsp";
            })
        }
    })
}

//选座头部进度条下面文字变色
function progress_text_color(){
    var scheduleId=$(".schedule").text();
    $.ajax({
        type: "get",
        url: "seat/getSelledseat?scheduleId="+scheduleId,
        success: function (result){
            console.log(result)
            var seatArr=result.data;
            console.log("seatArr:"+seatArr)
            var childreArr1=document.querySelector('.seat-wrapper').children;
            console.log(childreArr1)
            for(var i=0;i<10;i++){
                for(var j=0;j<10;j++){
                    if(seatArr[i][j]==1){
                        childreArr1[i].children[j].setAttribute("class","unselectable");
                    }
                }}
        }
    })
}

//页面加载时执行
progress_text_color();

//事件冒泡
document.querySelector(".seat-wrapper").onclick=function(event){
    console.log("///////////////////////////////////////////")
    var element=event.target;//事件作用的元素
    var nodename=element.nodeName;//标签名称
    console.log(nodename+"111111")


    var ticketsFlag=document.querySelector(".ticket-container");
    var ticketschildrenFlag=ticketsFlag.children;
    var ticketsNumFlag=ticketschildrenFlag.length;
    console.log("已经选了几张票："+ticketsNumFlag)


    if(ticketsNumFlag==6&&element.className=="seat selectable")
        window.alert("一次最多只能选购6张票!")

    //这边要先判断右边有几张票，不能放最下面，最后再去判断有几张票，不然右侧会少显示一张票
    var rowArr=document.querySelector(".seat-wrapper").children;
    var seatArr=rowArr.children;
    if(element.className=="unselectable"||element.className=="selecting"||element.className=="seat selectable"&&ticketsNumFlag!=6){
        if(element.className!="unselectable"&&element.className!="selecting")
            element.className="selecting";
        else if(element.className=="unselectable")
            element.className="unselectable"
        else
            element.className="seat selectable";
    }



//selecting变为seat selectable，seat selectable变为selecting
    console.log("element.className:"+element.className)


    //    定位位置坐标
    if(element.className=="seat selectable"||element.className=="selecting"){
        var child=element.previousSibling;
        var i1=0;
        while((child=child.previousSibling)!=null){
            console.log(child)
            i1++;
        }
        var parent=element.parentNode.previousSibling;
        var j1=0;
        while((parent=parent.previousSibling)!=null)j1++;
        // parent.previousElementSibling;用这个不用除以2
        console.log("i1:")
        console.log(i1)
        console.log("j1:"+j1)
        var ii=(i1+2)/2;
        var jj=(j1+2)/2;

        var tickets0=document.querySelector(".ticket-container");
        var tickets0children=tickets0.children;
        var ticketsLength=tickets0children.length;
        //console.log("tickets0children:"+tickets0children)
        if(element.className=="selecting"&&ticketsLength<6){
            var ticketnone1=document.querySelector(".ticket-container");
            var ticketn=document.querySelector(".has-ticket");
            ticketn.style.display="none";
            var tickets=document.querySelector(".has-ticket");
            tickets.style.display="block";
            var Ticketele='<span class="ticket">'
                +jj+'排'+ii+'座</span>';
            ticketnone1.innerHTML+=Ticketele;
            if(document.querySelector(".confirm-btn.disable")!=null){
                var confirmbtn=document.querySelector(".confirm-btn.disable");
                console.log("confirmbtn:"+confirmbtn)
                confirmbtn.className="confirm-btn";
            }
        }
        if(element.className=="seat selectable"){
            var tickets1=document.querySelector(".ticket-container");
            var dataIndex=jj+"排"+ii+"座";
            console.log("dataIndex:"+dataIndex)
            var tickets02children=tickets1.children;
            var ticketsNum02=tickets02children.length;
            for(var i=0;i<ticketsNum02;i++){
                if(tickets02children[i].textContent==dataIndex){
                    console.log("文本:"+tickets02children[i].textContent)
                    tickets02children[i].remove();
                    break;
                }
            }}
        //再获取一次票数，第一次获取时还没有加票数,这是最终票数
        var tickets01=document.querySelector(".ticket-container");
        var tickets01children=tickets01.children;
        var ticketsNum=tickets01children.length;
        console.log("选了几张票："+ticketsNum)
        var price=document.querySelector(".price");
        var schedulePrice=$(".schedulePrice").text();
        price.textContent=""+ticketsNum*schedulePrice;

        if(ticketsNum==0){
            var ticketdisplay=document.querySelector(".has-ticket");
            ticketdisplay.style.display="none";
            var ticketnone=document.querySelector(".no-ticket");
            ticketnone.style.display="block";
            var confirmbtn=document.querySelector(".confirm-btn");
            confirmbtn.className="confirm-btn disable";
        }
        else {
            var ticketdisplay11=document.querySelector(".has-ticket");
            ticketdisplay11.style.display="block";
            var ticketnone11=document.querySelector(".no-ticket");
            ticketnone11.style.display="none";
        }
    }
}


$(".confirm-btn").click(function (){
    var movieTicketInfo=new Array();
    var list=document.getElementsByClassName("value text-ellipsis");
    for(var i=0;i<list.length;i++){
        movieTicketInfo[i]=list[i].innerHTML;
    }
    movieTicketInfo[list.length]=customerId;
    console.log(movieTicketInfo)
    var movieTicketNum=new Array();
    var ticketlist=document.getElementsByClassName("ticket");
    for(var i=0;i<ticketlist.length;i++){
        movieTicketNum[i]=ticketlist[i].innerHTML;
    }
    console.log(movieTicketNum)
    $.ajax({
        type: "post",
        url: "seat/buytickets",
        data: {
            movieTicketInfo: movieTicketInfo,
            movieTicketNum: movieTicketNum
        },
        success:function (result){
            if (result.resultCode===200){
                var map=result.data;
                console.log("成功");
                console.log("movieTicketInfo"+map.movieTicketInfo);
                console.log("movieTicketNum"+map.movieTicketNum);
                localStorage.setItem("movieTicketInfo",map.movieTicketInfo);
                localStorage.setItem("movieTicketNum",map.movieTicketNum);
                localStorage.setItem("orderDate",map.orderDate);
                window.location.href = "pages/confirm.jsp";
            }else if(result.resultCode===201){
                console.log("失败");
                window.alert("座位已被购买,请重新选择！")
                window.location.href = "http://localhost:8080/demo/MovieSeat?scheduleId="+movieTicketInfo[6];
            }

        }
    })
})